<?php 
 require("../../config.php");
 $identreprise=$_GET['$ids'];

 if(isset($_POST['metier']) && isset($_POST['niveau']) && isset($_POST['competence']))
 {  
	$metier=$_POST['metier'];
	$competence=$_POST['competence'];
	$niveau= $_POST['niveau'];
	$prio=$_POST['prio'];
  echo "prio = $prio <br/>";  
     $req1="select idrecherche from recherche where date='0000-00-00 00:00:00' and identreprise=$identreprise"; //recuper le id de la table recherche sans date
     $req2="INSERT INTO  recherche(idrecherche, identreprise, date) VALUES ( NULL , '$identreprise', ''); "    ;   
     $result = mysql_query($req1) ;
      while($ligne = mysql_fetch_array($result)) {
  						$idRech = $ligne['idrecherche'];
              echo "$idRech</br>";
  		}
     
      if(empty($idRech))
      {
        //il n'y a pas infos
        if (!mysql_query($req2))
	       {
	         die('Error1: ' . mysql_error());
      	 }
      	$lastId = mysql_insert_id();
       	$requet2 ="INSERT INTO  recherche_competence(idcompetence, idrecherche, niveau, prio)  VALUES ( $competence, $lastId, '$niveau','$prio')";
	      if (!mysql_query($requet2))
      	{
      	 die('Error2: ' . mysql_error());
      	} 
      }
      else
      {     
	      $requet2 ="INSERT INTO  recherche_competence(idcompetence, idrecherche, niveau, prio)  VALUES ( $competence, $idRech, '$niveau','$prio')";
	      if (!mysql_query($requet2))
      	{
      	 die('Error3: ' . mysql_error());
      	} 
      }
      header('Location: ./rechercheCandidats.php ');  
}
?>  